package com.seo.service.keyword.member.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.seo.mapper.keyword.KeywordGMapper;
import com.seo.mapper.keyword.member.KeywordGMemberMapper;
import com.seo.pojo.KeyWord;
import com.seo.pojo.ReturnBody;
import com.seo.service.keyword.member.sdk.KeywordGMemberService;
import com.seo.utils.DateUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class KeywordGMemberServiceImpl implements KeywordGMemberService {

	@Autowired
	private KeywordGMemberMapper keywordGMemberMapper;

	/**
	 * 会员关键词列表
	 */
	public List<Map<String, Object>> getKeyWordsList(Map<String, Object> pam) {
		log.info("进入关键词列表service：");
		List<Map<String, Object>> keyWordsList = keywordGMemberMapper.getKeyWordsList(pam);
		log.info("关键词列表查询数据：" + keyWordsList);
		return keyWordsList;
	}

	/**
	 * 会员审核列表
	 */
	public List<Map<String, Object>> getKeyWordsReviewList(Map<String, Object> pam) {
		log.info("进入关键词审核列表service：");
		List<Map<String, Object>> keyWordsReviewList = keywordGMemberMapper.getKeyWordsReviewList(pam);
		log.info("关键词审核列表查询数据：" + keyWordsReviewList);

		return keyWordsReviewList;
	}

	/**
	 * 代理客户关键词列表
	 */
	public List<Map<String, Object>> getProxyKeyWordsList(Map<String, Object> pam) {
		log.info("进入关键词列表service：");
		List<Map<String, Object>> keyWordsList = keywordGMemberMapper.getProxyKeyWordsList(pam);
		log.info("关键词列表查询数据：" + keyWordsList);
		return keyWordsList;
	}

	/**
	 * 代理客户关键字审核列表
	 */
	public List<Map<String, Object>> getProxyKeyWordsReviewList(Map<String, Object> pam) {
		log.info("进入关键词审核列表service：");
		List<Map<String, Object>> keyWordsReviewList = keywordGMemberMapper.getProxyKeyWordsReviewList(pam);
		log.info("关键词审核列表查询数据：" + keyWordsReviewList);

		return keyWordsReviewList;
	}

	/**
	 * 获取厂商
	 */
	public List<Map<String, Object>> getVenderList() {
		return keywordGMemberMapper.getVenderList();
	}

	/**
	 * 关键词审核
	 */
	public void updatekeyWordReview(Map<String, Object> pam) {
		try {
			keywordGMemberMapper.updateProxykeyWordReviewDisabled(pam);
			keywordGMemberMapper.updatekeyWordReviewDisabled(pam);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 分组
	 */
	public ReturnBody updateGroupById(Map<String, Object> pam) {
		ReturnBody rb = ReturnBody.init();
		try {
			int num = keywordGMemberMapper.updateGroupById(pam);
			if (num == 0) {
				return rb.rollback();
			}
			return rb.success();
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}
	}

	/**
	 * 根据用户id查询此用户所有分组
	 * 
	 * @param pam
	 * @return
	 */
	public List<Map<String, Object>> getGroupByUserId(Integer userId) {

		return keywordGMemberMapper.getGroupByUserId(userId);
	}

	/**
	 * 修改价格和类型
	 */
	public ReturnBody updatePriceAndTargetRanking(Map<String, Object> pam) {
		ReturnBody rb = ReturnBody.init();
		try {
			int num = keywordGMemberMapper.updatePriceAndTargetRanking(pam);
			if (num == 0) {
				return rb.rollback();
			}
			return rb.success();
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}
	}

	/**
	 * 关键词停用启用
	 */
	public void updatekeyWordReviewDisabled(Map<String, Object> pam) {
		try {

			String ids = (String) pam.get("ids");
			if (StringUtils.isEmpty(ids)) {
				log.error("请输入ID");
				return;
			}

			String idArr[] = ids.split(",");
			if (idArr == null || idArr.length == 0) {
				log.error("请输入ID");
				return;
			}
			
			if(!pam.containsKey("price1")){
				log.error("价格不存在");
				return;
			}
			BigDecimal price1 = (BigDecimal)pam.get("price1");
			for (String id : idArr) {
				pam.clear();
				pam.put("ids",id);
				Map<String, Object> keyWordInfo = keywordGMemberMapper.getProxyKeyWordsInfo(pam);
				Long keyWordId = (Long) keyWordInfo.get("keywords_id");
				if (keyWordId != null) { // 说名 在表seo_keywords_user中有子表中的数据
											// 只需要修改状态修改为待审核状态就可以
					Map<String, Object> reviewMap = new HashMap<String, Object>();
					reviewMap.put("state", "00");
					reviewMap.put("ids", keyWordId);
					keywordGMemberMapper.updatekeyWordReview(reviewMap);
				} else { // 说明在表seo_keywords_user中没有字表中的数据 需要把字表中的数据添加到主表中
							// 添加完毕数据后 返回主表中的主键id 写入到集合proxyMap中的id,
					Map<String, Object> proxyMap = new HashMap<>();
					proxyMap.put("id", id);
					proxyMap.put("price1", price1);
					int num = keywordGMemberMapper.addKeyWordUser(proxyMap);
					if (num > 0) { // 说明添加成功 添加成功后 把表seo_proxy_keywords_user中的
									// keyword_id修改为主表中的id
						pam.put("keywords_id", proxyMap.get("id"));
						keywordGMemberMapper.updateKeyWordId(pam);
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

}
